<?php
namespace Admin\Controller;
class ArticleController extends CommonController{
    public function _initialize() {
		parent::_initialize();
        $this->_mod = D('Article');
        $this->_cate_mod = D('ArticleCate');
    }

    public function _before_index() {
		$this->list_relation = true;
        $p = I('p',1,'intval');
        $this->assign('p',$p);
    }

    protected function _search() {
        $map = array();
        ($time_start = I('get.time_start')) && $map['add_time'][] = array('egt', strtotime($time_start));
        ($time_end = I('get.time_end')) && $map['add_time'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($keyword = I('get.keyword')) && $map['title'] = array('like', '%'.$keyword.'%');
        $cate_id = I('get.cate_id');
		if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        $selected_ids = '';
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$cate_id))->getField('spid');
            $selected_ids = $spid ? $spid . $cate_id : $cate_id;
        }

        $map['site_id'] = array('eq',$this->siteId);
        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'cate_id' => $cate_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }

    public function _before_add(){

        $first_cate = $this->_cate_mod->field('id,name')->where(array('pid'=>0))->order('ordid DESC')->select();
        $this->assign('first_cate',$first_cate);

    }

    protected function _before_insert($data) {
        //自动提取摘要
        if($data['desc'] == '' && isset($data['info'])) {
            $content = stripslashes($data['info']);
            $data['desc'] = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
            $data['desc'] = addslashes($data['desc']);
        }

        $data['editor'] = $_SESSION['username'];
        $data['site_id'] = $this->siteId;
        $data['cate_alias'] = getCateAlias($data['cate_id']);
        return $this->_insertPic($data,'img',1);
    }

    public function _after_insert($id){

    }
    public function _before_edit(){
        $id = I('get.id');
        $article = $this->_mod->field('id,cate_id,file')->where(array('id'=>$id))->find();
        $spid = $this->_cate_mod->where(array('id'=>$article['cate_id']))->getField('spid');
        if( $spid==0 ){
            $spid = $article['cate_id'];
        }else{
            $spid .= $article['cate_id'];
        }
        $file = json_decode($article['file'],true);
        if(!empty($file) && is_array($file)){
            $this->imglist = $file;
        }


        $this->assign('selected_ids',$spid);
    }

    protected function _before_update($data) {
        //自动提取摘要
        if($data['desc'] == '' && isset($data['info'])) {
            $content = stripslashes($data['info']);
            $data['desc'] = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
            $data['desc'] = addslashes($data['desc']);
        }
        $data['cate_alias'] = getCateAlias($data['cate_id']);
        $newdata = $this->_updatePic($data);
        if($data['isrec'] == 1){
            $updata = array('title'=>$data['title'],'img'=>$newdata['img'],'id'=>$data['id']);
            D('Position') -> where(array('aid'=>$data['id'],'model'=>'article'))->save(array('data'=>serialize($updata)));
        }
        return $newdata;
    }
    public function publicSelectArticle(){
        $this->type = I('get.type') ? I('get.type') : 'radio';
        $where = array('status'=>1,'site_id'=>$this->siteId,'add_time'=>array('lt',time()));
        $this->result = $this->_mod->where($where)->order('ordid desc,id desc')->field('id,title,img')->limit(24)->select();
        $count   = $this->_mod->where($where)->count();
        $pageObj = new \Org\Util\Page($count,24);
        $this->pages=$pageObj->ajaxPage(1);
        $response = $this->fetch('selectarticle');
        $this->ajaxReturn(1,'',$response);
    }
    public function ajaxSelectArticle(){
        $pagesize = 24;
        $cateid = I('get.cate_id',0,'int');
        $site_id = I('get.site_id',$this->siteId,'int');
        $q      = I('get.q');
        $page   = I('get.page',1,'int');
        $where  = array();
        if($cateid){
            $id_arr = $this->_cate_mod->get_child_ids($cateid, true);
            $where['cate_id'] = array('IN', $id_arr);
        }
        if($site_id){
            $where['site_id'] = array('eq',$site_id);
        }
        if($q){
            $where['title'] = array('like','%'.$q.'%');
        }
        $where['add_time'] = array('lt',time());
        // $productObj = M('product');
        //分页类
        $count   = $this->_mod->where($where)->count();
        $pageObj = new \Org\Util\Page($count,$pagesize);
        $result = $this->_mod->where($where)->field('id,img,title')->order('ordid desc,id desc')->page($page,$pageObj->listRows)->select();
        $res    = array('page'=>$pageObj->ajaxPage($page),'data'=>$result);
        $this->ajaxReturn(1,'',$res);
    }

}